package cn.itsy.web.controller;

import cn.itsy.dao.Rehireddao;
import cn.itsy.pojo.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("rehired")
public class RehiredController {
    @Resource
    private Rehireddao rd;

    @ResponseBody
    @RequestMapping("list")
    public List<Emp> list(Emp emp, @RequestParam Map<String, Object> map) {
        String sql = "select em.*,j.name,d.zname,dep.dname from emp em,job j,duct d,empstatus esta, dept dep " +
                " where em.job_id=j.id  and em.duct_id=d.id and em.is_status=esta.id and em.depart_id=dep.id and em.is_status='8'";
        if (emp.getEmpname() != null && !emp.getEmpname().equals("")) {
            sql += " and  em.empname like '%" + emp.getEmpname() + "%' ";
        }

        if (emp.getSex() != null && !emp.getSex().equals("")) {
            sql += " and em.sex='" + emp.getSex() + "' ";
        }

        if (emp.getEducation() != null && !emp.getEducation().equals("")) {
            sql += " and em.education='" + emp.getEducation() + "' ";
        }

        if (emp.getDepartId() != null && !emp.getDepartId().equals("")) {
            sql += " and em.depart_id='" + emp.getDepartId() + "' ";
        }

        if (emp.getDuctId() != null && !emp.getDuctId().equals("")) {
            sql += " and em.duct_id='" + emp.getDuctId() + "' ";
        }

        if (emp.getJobId() != null && !emp.getJobId().equals("")) {
            sql += " and em.job_id='" + emp.getJobId() + "' ";
        }
        List<Emp> empsize = rd.list(sql);
        Integer is = Integer.valueOf(map.get("i").toString());
        //每页显示多少行
        Integer isum = 10;
        //总页数
        Integer count = empsize.size() % isum > 0 ? empsize.size() / isum + 1 : empsize.size() / isum;

        //判断点击的是哪一个按钮
        if (map.get("state").equals("首页")) {
            is = 1;
        }
        if (map.get("state").equals("上一页")) {
            is = is <= 1 ? 1 : is - 1;
        }
        if (map.get("state").equals("下一页")) {
            is = is >= count ? count : is + 1;
        }
        if (map.get("state").equals("尾页")) {
            is = count;
        }
        if (map.get("state").equals("跳转")) {
            if (map.get("reverse") != null && !map.get("reverse").equals("")) {
                System.out.println(map.get("reverse") + "=-=-=-=-=-=-=-=-=-==-=-=");
                Integer a = new Integer((String) map.get("reverse"));
                if (a > count) {
                    is = count;
                }
                if (a < count) {
                    is = Integer.valueOf(map.get("reverse").toString());
                }
            }
        }
        int isx = (is - 1) * isum;
        sql += " limit " + isx + "," + isum + " ";
        List<Emp> list = rd.list(sql);
        return list;
    }

    @ResponseBody
    @RequestMapping("deptlist")
    public List<Dept> deptlist() {
        String sql = "select * from dept";
        List<Dept> deptlist = rd.deptlist(sql);
        return deptlist;
    }

    @ResponseBody
    @RequestMapping("ductlist")
    public List<Duct> ductlist() {
        String sql = "select * from duct";
        List<Duct> ductlist = rd.ductlist(sql);
        return ductlist;
    }

    @ResponseBody
    @RequestMapping("joblist")
    public List<Job> joblist() {
        String sql = "select * from job";
        List<Job> joblist = rd.joblist(sql);
        return joblist;
    }

    @ResponseBody
    @RequestMapping("select")
    public List<Emp> select(String empid) {
        String sql = "select em.*,j.name,d.zname,dep.dname from emp em,job j,duct d,empstatus esta, dept dep " +
                " where em.job_id=j.id  and em.duct_id=d.id and em.is_status=esta.id and em.depart_id=dep.id and esta.id='8' and em.empid='" + empid + "'";
        List<Emp> list = rd.list(sql);
        return list;
    }

    @RequestMapping("update")
    public String update(String empid, Model model) {
        String sql = "select em.*,j.name,d.zname,dep.dname from emp em,job j,duct d,empstatus esta, dept dep " +
                " where em.job_id=j.id  and em.duct_id=d.id and em.is_status=esta.id and em.depart_id=dep.id and esta.id='8' and em.id='" + empid + "'";
        List<Emp> list = rd.list(sql);
        model.addAttribute("list", list);
        return "/rehiredupdate";
    }

    @RequestMapping("updateemp")                                    //txt文本域的值
    public String updateShow(EmpJobDuctEmpstatus emp, @RequestParam String txt) {
        System.out.println("emp集合:" + emp.toString());
        //当前时间
        Date date = new Date();
        String sj = date.toLocaleString();
        String str = "辞职";
        //往emphistory表中添加辞职记录
        String insertsql = "insert into emphistory values(default,'" + emp.getId() + "','" + sj + "','" + txt + "','" + str + "')";
        rd.insertEmpHistoryShow(insertsql);
        //修改emp表中的is_status字段（状态）
        String updatesql = "update emp set is_status='" + 5 + "' where id='" + emp.getId() + "'";
        rd.updateEmpHistoryShow(updatesql);

        return "/rehired";
    }
}
